package com.google.coffeeshop.gui.desktop.orderpayment;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

import com.google.coffeeshop.GeneralConstants;
import com.google.coffeeshop.business.LoadingBusiness;
import com.google.coffeeshop.business.OrderBusiness;
import com.google.coffeeshop.entity.EmployeeDTO;
import com.google.coffeeshop.entity.SeatDTO;
import com.google.coffeeshop.gui.desktop.orderpayment.seatdetail.SeatDetailFrame;

public class SeatBoardPanel extends JPanel {
	private static final long serialVersionUID = 1L;

	private OrderPaymentActionsPanel orderPaymentActionsPanel;
	private OrderPaymentPanel parent;
	
	public SeatBoardPanel(OrderPaymentPanel parent, OrderPaymentActionsPanel orderPaymentActionsPanel) {
		this.parent = parent;
		this.orderPaymentActionsPanel = orderPaymentActionsPanel;
		this.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		FlowLayout flowLayout = (FlowLayout) this.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		showSeats();
	}
	
	public void showSeats() {
		for (SeatDTO seat : LoadingBusiness.getSeatsDefault()) {
			initSeat(this, seat);
		}
	}
	
	private void initSeat(JPanel panel, SeatDTO seat) {
		SeatButton btnSeat = new SeatButton(seat);
		btnSeat.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseMoved(MouseEvent e) {
				SeatButton seatButton = (SeatButton)e.getSource();
				parent.showSeatOverview(seatButton.getSeat());
			}
		});
		btnSeat.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SeatButton seatButton = (SeatButton)e.getSource();
				if (seatButton.getSeat().getStatus().equals(GeneralConstants.SEAT_STOP_STATUS)) {
					showStopSeatActionsFrame(seatButton);
				} else if (seatButton.getSeat().getStatus().equals(GeneralConstants.SEAT_USABLE_STATUS)) {
					showSeatUsingFrame(seatButton);
				} else if (seatButton.getSeat().getStatus().equals(GeneralConstants.SEAT_BUSY_STATUS)) {
					showSeatDetailFrame(seatButton, null);
				}
			}
		});
		panel.add(btnSeat);
	}
	
	public void refreshGUI() {
		removeAllSeatButtons();
		showSeats();
	}
	
	
	public void refreshGuiAndParent() {
		parent.refreshGUI();
	}

	private void removeAllSeatButtons() {
		removeAll();
	}
	
	private void showSeatUsingFrame(SeatButton seatButton) {
		EmployeeChoosingFrame frmSeatUsing = new EmployeeChoosingFrame(this, seatButton);
		frmSeatUsing.setVisible(Boolean.TRUE);
	}
	
	private void showStopSeatActionsFrame(SeatButton seatButton) {
		StopSeatActionsFrame frmStopSeatActions = new StopSeatActionsFrame(this, seatButton.getSeat());
		frmStopSeatActions.setVisible(Boolean.TRUE);
	}
	
	public void showSeatDetailFrame(SeatButton seatButton, Long employeeId) {
		if (employeeId == null) {
			if (seatButton != null && seatButton.getSeat() != null) {
				employeeId = getEmployeeId(seatButton.getSeat().getId());
			}
		}
		SeatDetailFrame frmSeatDetail = new SeatDetailFrame(this, seatButton.getSeat(), employeeId);
		frmSeatDetail.setVisible(Boolean.TRUE);
	}
	
	private Long getEmployeeId(long seatId) {
		EmployeeDTO employee = OrderBusiness.findEmployeeBySeatId(seatId);
		if (employee != null) {
			return employee.getId();
		}
		return null;
	}
	
}
